from PyXSE.Schema import Schema

class TableSchema(Schema):

    """TableSchema is an interface for the Table XML file."""

    schema_label = u'table'
    base_file_name = 'table_schema.xml'
    schema_entry_label = u'column'
    supported_attributes = ['id', 'type', 'unique']
    
    def list_column_ids(self):
        """Lists the ids of the table's columns."""
        return Schema._list_attributes(self, 'id')

    def list_column_types(self):
        """Lists the types of the table's columns."""
        return Schema._list_attributes(self, 'type')

    def list_unique_column_ids(self):
        """Lists the ids of the table's unique columns."""
        return [x for x in self.list_column_ids() \
                            if self._list_attribute(x, 'unique') == 'true']

    def add_column(self, column_id, column_type, unique=False):
        """Adds a column to this table.

        column_id:   a string representing the id of the column.
        column_type: a string representing the type of the column.
        unique:      an optional boolean that, if given, ensures that
                     the values for this column are unique.

        """
        vals = [column_id, column_type]
        if unique:
            vals.append('true')
        else:
            vals.append('false')
        Schema._add(self, vals)

    def drop_column(self, column_id):
        """Deletes a column from this table.

        column_id: a string representing the id of the column.

        """
        Schema._drop(self, column_id)

    def set_column_type(self, column_id, column_type):
        """Sets a column's type.

        column_id: a string representing the id of the column.
        column_type: a string representing the new type of the column.

        """
        Schema._set_attribute(self, column_id, 'type', column_type)

    def set_column_name(self, column_name, new_name):
        """Sets a column's name.

        column_name: a string representing the name of the column.
        new_name:    a string representing the new name of the column.

        """
        Schema._rename(self, column_name, new_name)

